﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Yunchang;

namespace AutoChess
{
    public class FightEntityCtr
    {
        private Dictionary<long, FightEntity> fightEntityMap = new Dictionary<long, FightEntity>();
        /// <summary> 一次战斗阶段任何观战 可能卡住的时候的超时时间 通知战斗内部退出 </summary>
        private float fightStateOutTime = 0;

        /// <summary> 正常模拟战斗的30秒时间 </summary>
        private float curFightPassTime = 0;

        /// <summary> 是否计算战斗流逝时间</summary>
        private bool isCalcFightTime = false;

        /// <summary>观战过程中 整体的战斗状态阶段  当当前走到战斗阶段的时候 会根据每个人的数据 看战斗是否需要进入战斗</summary>
        private FightState cacheFightState = FightState.None;

        private FightEntity curEntity;
        public Action onFightPassTimeOut;
        //切换战斗的时间
        public float EnterNormalizeFightTime
        {
            get { return Table.DataModel.TableConfig.AutoChessFightDurationTime - curFightPassTime; }
        }
        public float FightStateOutTime //超时时间
        {
            get { return fightStateOutTime; }
        }
        public FightEntity CurFightEntity
        {
            get { return curEntity; }
        }
        public FightState CacheFightState
        {
            get { return cacheFightState; }
            set { cacheFightState = value; }
        }
        public bool CalcFightTimeState
        {
            get { return isCalcFightTime;}
            set { isCalcFightTime = value; }
        }
        public float CurFightPassTime//当前已经流逝的战斗时间
        {
            get{ return curFightPassTime; }
        }
        public void SwitchFightEntity(long  playerGuid)
        {
            curEntity = fightEntityMap[playerGuid];
        }
        public void OnCreateAllFightEntity()
        {
            fightEntityMap.Clear();
            var allData = AutoChessManager.Ins.cacheData.GetAllPlayerList();
            for (int i = 0; i < allData.Count; i++)
            {
                var fightEntity = new FightEntity();
                fightEntity.OnFightCreate(allData[i].GetFightPackage(),this);
                fightEntityMap[allData[i].GUID] = fightEntity;
            }
            fightStateOutTime = Table.DataModel.TableConfig.AutoChessFightOutTime;//战斗超时时间，到了这个时间 所有玩家都是 结果状态
            curFightPassTime = Table.DataModel.TableConfig.AutoChessFightDurationTime;  //战斗UI表现上显示的30秒 ,切换过程等的时候 这个时间需求需要暂停，等切换加载完开始回合后 重新开始计时
            isCalcFightTime = false;
        }
        public void AllFightEntityFinished()
        {
            var itor = fightEntityMap.GetEnumerator();
            while (itor.MoveNext())
            {
                if(itor.Current.Value.PlayerGUID !=curEntity.PlayerGUID)
                    itor.Current.Value.ComplateFightFinished();
            }
        }
        public void OnTickFightEntty()
        {
            var itor = fightEntityMap.GetEnumerator();
            while (itor.MoveNext())
            {
                itor.Current.Value.OnFightTick();
            }
            if (isCalcFightTime)
            {
                curFightPassTime -= Time.unscaledDeltaTime;
            }
            if (curFightPassTime <= 0) //战斗展示时间到了
            {
                if (onFightPassTimeOut != null)
                {
                    onFightPassTimeOut();
                    onFightPassTimeOut = null;
                }
            }
            fightStateOutTime -= Time.unscaledDeltaTime;
        }

        public void OnDestroyAllFightEntity()
        {
            var itor = fightEntityMap.GetEnumerator();
            while (itor.MoveNext())
            {
                itor.Current.Value.OnDestroyFight();
            }
            fightStateOutTime = 0;
            curFightPassTime = 0;
        }
       
        internal void Release()
        {
        }
    }
}
